package com.xiaoxiong.text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.xiaoxiong.database.OperateFormSyllabus;

import android.content.Context;
import android.util.Log;

public class DealCourseDataByRegular {
	private Context context;
	private String html;
	private List<String> courseMess = new ArrayList<String>();// 存每节课信息
	private List<Map<String, String>> mapCourseMess = new ArrayList<Map<String, String>>();// 存分离好的课程信息
	private Map<String, String> CourseName = new HashMap<String, String>();// 存已处理过的课程名，以便处理颜色

	private int thisWeek;
	private int color = 0;

	public DealCourseDataByRegular(Context context, String html) {
		super();
		this.html = html;
		this.context = context;
		DealData();
	}

	public int getsv() {
		return thisWeek;
	}

	private void DealData() {
		Document content = Jsoup.parse(html);
		Elements element = content.getElementsByClass("font12");

		Elements element2 = content.getElementsByClass("style16");

		String we = element2.get(element2.size() - 1).text();
		thisWeek = Integer.parseInt(we.replaceAll("\\D", ""));
		// Log.d("title", we.replaceAll("\\D", ""));

		StringBuilder sb3 = new StringBuilder();
		String[] result = new String[element.size()];
		int p = 0;
		for (Element links : element) {
			String title = links.text();
			result[p++] = title;
			// Log.d("title", title);
		}
		int k = 0;
		String time = "";
		color = 0;
		for (int i = 8; i < result.length; ++i) {
			if (k == 0) {
				// 代表第一格的时间
				time = result[i];
				// Log.d("html", time);
			} else if (result[i].length() > 4) {// 因为空白的格子没有数据，而有数据的格子字符串长度一定大于4，所以我们取4来做判断就够了
				Map<String, String> map = serializeCourse(result[i]);
				map.put("week", k + "");
				map.put("starttime", time.substring(0, time.indexOf(" ")));
				map.put("time", time);

				mapCourseMess.add(map);

				if (result[i].contains(",")) {
					map = serializeCourse(result[i].substring(
							result[i].indexOf(",") + 1, result[i].length()));
					map.put("week", k + "");
					map.put("starttime", time.substring(0, time.indexOf(" ")));
					map.put("time", time);
					// Log.d("---", "coursename:" + map.get("coursename"));
					// Log.d("---", "time:" + map.get("time"));
					// Log.d("---", "teacher:" + map.get("teacher"));
					// Log.d("---", "room:" + map.get("room"));
					// Log.d("---", "weeks:" + map.get("weeks"));
					// Log.d("---", "week:" + map.get("week"));
					// Log.d("---", "starttime:" + map.get("starttime"));
					// Log.d("---", "color:" + map.get("color"));
					// Log.d("---", "---------------\n");

					mapCourseMess.add(map);

				}
			}
			k++;
			if (k == 8) {
				k = 0;
			}
		}
		setCourseData();
	}

	// 分离课程数据
	private Map<String, String> serializeCourse(String value) {
		
		Log.d("value", value);

		Map<String, String> map = new HashMap<String, String>();
		String str = value.replaceAll(" ", "@");
		// 获取课程名字
		String coursename = str.substring(0, value.indexOf("(")).replaceAll(
				"@", "");
		map.put("coursename", coursename);

		String m = str.substring(str.indexOf("(") + 1, str.indexOf(")"))
				.replaceFirst("@", " ");
		// 获取老师名字跟教学班
		String teacher = m.substring(0, m.indexOf("@"));
		map.put("teacher", teacher);

		// 获取教室
		String room = str.substring(str.indexOf("[") + 1, str.indexOf("]"));
		map.put("room", room.toString());

		// 获取上课周
		String weeks = m.substring(m.indexOf("@") + 1, m.indexOf("周"))
				.replaceAll("@", " ");
		map.put("weeks", weeks);

		if (CourseName.containsKey(coursename)) {
			map.put("color", CourseName.get(coursename));
			// Log.d("--**-", coursename + " " + CourseName.get(coursename));
		} else {
			map.put("color", String.valueOf(color));
			CourseName.put(coursename, String.valueOf(color));
			// Log.d("--**-", coursename + " " + color);
			color++;
		}

		return map;
	}

	// 保存数据
	public void setCourseData() {
		try {
			int id = 0;
			Map<String, String> mapcourse = new HashMap<String, String>();
			OperateFormSyllabus syllabus = new OperateFormSyllabus(context);
			syllabus.deleteAllSaysay();
			while (!mapCourseMess.isEmpty()) {
				mapcourse = mapCourseMess.get(0);
				mapCourseMess.remove(0);

				syllabus.insertSyllabus(mapcourse.get("coursename"),
						mapcourse.get("time"), mapcourse.get("teacher"),
						mapcourse.get("room"), mapcourse.get("weeks"),
						Integer.parseInt(mapcourse.get("week")),
						Integer.parseInt(mapcourse.get("starttime")),
						Integer.parseInt(mapcourse.get("color")), id++);

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
